package com.jtpay.unionpay.product.utils;

import com.jtpay.unionpay.product.utils.manager.ExceptionUtil;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * ActivityHelper
 * The following operation need  to be performed in the root environment
 */
public class ActivityHelper {

    private static final String unionPayService = "settings put secure " +
            "enabled_accessibility_services com.jtpay.unionpay.product/" +
            "com.jtpay.unionpay.product.service.UnionPayProductService";

    private static final String openService = "settings put secure accessibility_enabled 1";

    private final static String UP_CALLBACK_PACKAGE_NAME = "com.jtpay.unionpay.product";
    private final static String UP_CALLBACK_MAIN_ACTIVITY = UP_CALLBACK_PACKAGE_NAME
            + ".MainActivity";
    public final static String UNION_PAY_PACKAGE_NAME = "com.unionpay";
    public final static String UNION_PAY_REACT_NATIVE_ACTIVITY =
            UNION_PAY_PACKAGE_NAME + ".activity.react.UPReactNativeActivity";
    public final static String UNION_PAY_TRANS_DETAIL_ACTIVITY =
            UNION_PAY_PACKAGE_NAME + ".activity.account.UPActivityAccountTransDetail";
    public final static String UNION_PAY_MAIN_ACTIVITY =
            UNION_PAY_PACKAGE_NAME + ".activity.UPActivityMain";
    public final static String UNION_PAY_WEB_ACTIVITY =
            UNION_PAY_PACKAGE_NAME + ".cordova.UPActivityWeb";


    public final static String UNION_PAY_LAUNCHER = ".activity.UPActivityWelcome";

    public static void startUnionPayMainActivity() {
        startActivity(UNION_PAY_PACKAGE_NAME, UNION_PAY_MAIN_ACTIVITY);
//        startActivity(UNION_PAY_PACKAGE_NAME, UNION_PAY_LAUNCHER);
    }

    public static void startProductMainActivity() {
        startActivity(UP_CALLBACK_PACKAGE_NAME, UP_CALLBACK_MAIN_ACTIVITY);
    }

    public static String getTopActivity() {
        String cmd = "dumpsys activity | grep mFocusedActivity";
        //        MainActivity.instance.log("now activity:" + nowactivity);
        return execRootCmd(cmd);
    }

    public static Boolean topActivityEquals(String cName) {
        String cmd = "dumpsys activity | grep mFocusedActivity";
        String nowActivity = execRootCmd(cmd);
//        MainActivity.instance.log("now activity:" + nowActivity);
        return nowActivity.contains(cName);
    }

    /**
     * auto start AccessibilityService
     */
    public static void openUnionPayService() {
        execRootCmd(unionPayService);
        execRootCmd(openService);
    }

    /**
     * auto start screenShot
     */
    public static void screenShot(String path, String name) {
        execRootCmd("screencap -p " + path + "/" + name + "\n");
    }

    /**
     * @param x click coordinate x
     * @param y click coordinate y
     */
    public static void inputClick(int x, int y) {
        execRootCmd(String.format("input tap %s %s", x, y));
    }

    /**
     * Simulated physical sliding
     *
     * @param startX Starting  x coordinate
     * @param startY Starting  y coordinate
     * @param endX   end  x coordinate
     * @param endY   end  x coordinate
     */
    public static void inputSwipe(int startX, int startY, int endX, int endY) {
        String cmd = String.format("input swipe %s %s %s %s", startX,
                startY, endX, endY);
        execRootCmd(cmd);
    }


    /**
     * Start specifying Activity
     *
     * @param packageName  packageName
     * @param activityName activityName
     */
    public static void startActivity(String packageName, String activityName) {
        String cmd = String.format("am start -n %s/%s", packageName,
                activityName);
        execRootCmd(cmd);
    }

    /**
     * Start specifying Service
     *
     * @param packageName packageName
     * @param serviceName serviceName
     */
    private static void startService(String packageName, String serviceName) {
        String cmd = String.format("am start service -n %s/%s", packageName,
                serviceName);
        execRootCmd(cmd);
    }

    private static String execRootCmd(String cmd) {
        StringBuilder result = new StringBuilder();
        DataOutputStream dos = null;
        DataInputStream dis = null;
        try {
            Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
            dos = new DataOutputStream(p.getOutputStream());
            dis = new DataInputStream(p.getInputStream());

            dos.writeBytes(cmd + "\n");
            dos.flush();
            dos.writeBytes("exit\n");
            dos.flush();
            String line;
            while ((line = dis.readLine()) != null) {
                result.append(line);
            }
            p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionUtil.getInstance().saveExceptionFile(
                    "ActivityHelper execRootCmd Exception = "
                            + e.getMessage());
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    ExceptionUtil.getInstance().saveExceptionFile(
                            "ActivityHelper execRootCmd " +
                                    "IOException = " + e.getMessage());
                }
            }
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    ExceptionUtil.getInstance().saveExceptionFile(
                            "ActivityHelper execRootCmd " +
                                    "IOException = " + e.getMessage());
                }
            }
        }
        return result.toString();
    }

}
